/*
  Copyright 2009 Smart Enterprise Solution Corp.
  Email: contact@ses.vn - Website: http://www.ses.vn
  KimNgan Project.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using SES.CMS.DO;
/// <summary>
/// Summary description for cmsGioHangDAL
/// </summary>
namespace SES.CMS.DAL
{

    public class cmsGioHangDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public cmsGioHangDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(cmsGioHangDO objcmsGioHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@SanPhamID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.SanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ClientLoginID", SqlDbType.Int);
            Sqlparam.Value = objcmsGioHangDO.ClientLoginID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SoLuong", SqlDbType.Int);
            Sqlparam.Value = objcmsGioHangDO.SoLuong;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@HinhThucThanhToan", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.HinhThucThanhToan;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ThanhTien", SqlDbType.Float);
            Sqlparam.Value = objcmsGioHangDO.ThanhTien;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayDatHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayDatHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayGiaoHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayGiaoHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@XuLy", SqlDbType.Bit);
            Sqlparam.Value = objcmsGioHangDO.XuLy;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Insert2(cmsGioHangDO objcmsGioHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@SanPhamID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.SanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ClientLoginID", SqlDbType.Int);
            Sqlparam.Value = 0;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SoLuong", SqlDbType.Int);
            Sqlparam.Value = objcmsGioHangDO.SoLuong;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@HinhThucThanhToan", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.HinhThucThanhToan;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ThanhTien", SqlDbType.Float);
            Sqlparam.Value = objcmsGioHangDO.ThanhTien;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayDatHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayDatHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayGiaoHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayGiaoHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@XuLy", SqlDbType.Bit);
            Sqlparam.Value = objcmsGioHangDO.XuLy;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(cmsGioHangDO objcmsGioHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@GioHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.GioHangID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SanPhamID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.SanPhamID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ClientLoginID", SqlDbType.Int);
            Sqlparam.Value = objcmsGioHangDO.ClientLoginID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SoLuong", SqlDbType.Int);
            Sqlparam.Value = objcmsGioHangDO.SoLuong;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@HinhThucThanhToan", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.HinhThucThanhToan;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ThanhTien", SqlDbType.Float);
            Sqlparam.Value = objcmsGioHangDO.ThanhTien;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.NVarChar);
            Sqlparam.Value = objcmsGioHangDO.SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayDatHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayDatHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@NgayGiaoHang", SqlDbType.DateTime);
            Sqlparam.Value = objcmsGioHangDO.NgayGiaoHang;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@XuLy", SqlDbType.Bit);
            Sqlparam.Value = objcmsGioHangDO.XuLy;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(cmsGioHangDO objcmsGioHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@GioHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.GioHangID;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public cmsGioHangDO Select(cmsGioHangDO objcmsGioHangDO)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@GioHangID", SqlDbType.BigInt);
            Sqlparam.Value = objcmsGioHangDO.GioHangID;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["GioHangID"]))
                    objcmsGioHangDO.GioHangID = Convert.ToInt64(dr["GioHangID"]);
                if (!Convert.IsDBNull(dr["SanPhamID"]))
                    objcmsGioHangDO.SanPhamID = Convert.ToInt64(dr["SanPhamID"]);
                if (!Convert.IsDBNull(dr["ClientLoginID"]))
                    objcmsGioHangDO.ClientLoginID = Convert.ToInt32(dr["ClientLoginID"]);
                if (!Convert.IsDBNull(dr["SoLuong"]))
                    objcmsGioHangDO.SoLuong = Convert.ToInt32(dr["SoLuong"]);
                if (!Convert.IsDBNull(dr["HinhThucThanhToan"]))
                    objcmsGioHangDO.HinhThucThanhToan = Convert.ToString(dr["HinhThucThanhToan"]);
                if (!Convert.IsDBNull(dr["ThanhTien"]))
                    objcmsGioHangDO.ThanhTien = Convert.ToInt32(dr["ThanhTien"]);
                if (!Convert.IsDBNull(dr["SessionID"]))
                    objcmsGioHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                if (!Convert.IsDBNull(dr["NgayDatHang"]))
                    objcmsGioHangDO.NgayDatHang = Convert.ToDateTime(dr["NgayDatHang"]);
                if (!Convert.IsDBNull(dr["NgayGiaoHang"]))
                    objcmsGioHangDO.NgayGiaoHang = Convert.ToDateTime(dr["NgayGiaoHang"]);
                if (!Convert.IsDBNull(dr["XuLy"]))
                    objcmsGioHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);

            }
            return objcmsGioHangDO;
        }
        public DataTable SelectBySessionChuaXL(string id)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_GetBySessionIDNonXL";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ID", SqlDbType.NVarChar);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;



        }

        public DataTable SelectBySession(string id)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "cmsChiTiet";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@ID", SqlDbType.NVarChar);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;


            
        }

        public DataTable SelectBySessionID(string session)
        {
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_GetBySessionID";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@SessionID", SqlDbType.NVarChar);
            Sqlparam.Value = session;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        public ArrayList SelectAll1()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrcmsGioHangDO = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cmsGioHangDO objcmsGioHangDO = new cmsGioHangDO();
                    if (!Convert.IsDBNull(dr["GioHangID"]))
                        objcmsGioHangDO.GioHangID = Convert.ToInt64(dr["GioHangID"]);
                    if (!Convert.IsDBNull(dr["SanPhamID"]))
                        objcmsGioHangDO.SanPhamID = Convert.ToInt64(dr["SanPhamID"]);
                    if (!Convert.IsDBNull(dr["ClientLoginID"]))
                        objcmsGioHangDO.ClientLoginID = Convert.ToInt32(dr["ClientLoginID"]);
                    if (!Convert.IsDBNull(dr["SoLuong"]))
                        objcmsGioHangDO.SoLuong = Convert.ToInt32(dr["SoLuong"]);
                    if (!Convert.IsDBNull(dr["HinhThucThanhToan"]))
                        objcmsGioHangDO.HinhThucThanhToan = Convert.ToString(dr["HinhThucThanhToan"]);
                    if (!Convert.IsDBNull(dr["ThanhTien"]))
                        objcmsGioHangDO.ThanhTien = Convert.ToInt32(dr["ThanhTien"]);
                    if (!Convert.IsDBNull(dr["SessionID"]))
                        objcmsGioHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                    if (!Convert.IsDBNull(dr["NgayDatHang"]))
                        objcmsGioHangDO.NgayDatHang = Convert.ToDateTime(dr["NgayDatHang"]);
                    if (!Convert.IsDBNull(dr["NgayGiaoHang"]))
                        objcmsGioHangDO.NgayGiaoHang = Convert.ToDateTime(dr["NgayGiaoHang"]);
                    if (!Convert.IsDBNull(dr["XuLy"]))
                        objcmsGioHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);
                    arrcmsGioHangDO.Add(objcmsGioHangDO);
                }
            }
            return arrcmsGioHangDO;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGioHang_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }

        public cmsGioHangDO SelecBySessionAndSP(string session, Int64 id)
        {
            cmsGioHangDO objcmsGioHangDO = new cmsGioHangDO();
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_cmsGioHang";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@session", SqlDbType.NVarChar);
            Sqlparam.Value = session;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@id", SqlDbType.BigInt);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["GioHangID"]))
                    objcmsGioHangDO.GioHangID = Convert.ToInt64(dr["GioHangID"]);
                if (!Convert.IsDBNull(dr["SanPhamID"]))
                    objcmsGioHangDO.SanPhamID = Convert.ToInt64(dr["SanPhamID"]);
                if (!Convert.IsDBNull(dr["ClientLoginID"]))
                    objcmsGioHangDO.ClientLoginID = Convert.ToInt32(dr["ClientLoginID"]);
                if (!Convert.IsDBNull(dr["SoLuong"]))
                    objcmsGioHangDO.SoLuong = Convert.ToInt32(dr["SoLuong"]);
                if (!Convert.IsDBNull(dr["HinhThucThanhToan"]))
                    objcmsGioHangDO.HinhThucThanhToan = Convert.ToString(dr["HinhThucThanhToan"]);
                if (!Convert.IsDBNull(dr["ThanhTien"]))
                    objcmsGioHangDO.ThanhTien = Convert.ToInt32(dr["ThanhTien"]);
                if (!Convert.IsDBNull(dr["SessionID"]))
                    objcmsGioHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                if (!Convert.IsDBNull(dr["NgayDatHang"]))
                    objcmsGioHangDO.NgayDatHang = Convert.ToDateTime(dr["NgayDatHang"]);
                if (!Convert.IsDBNull(dr["NgayGiaoHang"]))
                    objcmsGioHangDO.NgayGiaoHang = Convert.ToDateTime(dr["NgayGiaoHang"]);
                if (!Convert.IsDBNull(dr["XuLy"]))
                    objcmsGioHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);

            }
            return objcmsGioHangDO;
        }
        public cmsGioHangDO SelecBySessionAndSPNonXL(string session, Int64 id)
        {
            cmsGioHangDO objcmsGioHangDO = new cmsGioHangDO();
            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_cmsGioHangChuaXly";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@session", SqlDbType.NVarChar);
            Sqlparam.Value = session;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@id", SqlDbType.BigInt);
            Sqlparam.Value = id;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["GioHangID"]))
                    objcmsGioHangDO.GioHangID = Convert.ToInt64(dr["GioHangID"]);
                if (!Convert.IsDBNull(dr["SanPhamID"]))
                    objcmsGioHangDO.SanPhamID = Convert.ToInt64(dr["SanPhamID"]);
                if (!Convert.IsDBNull(dr["ClientLoginID"]))
                    objcmsGioHangDO.ClientLoginID = Convert.ToInt32(dr["ClientLoginID"]);
                if (!Convert.IsDBNull(dr["SoLuong"]))
                    objcmsGioHangDO.SoLuong = Convert.ToInt32(dr["SoLuong"]);
                if (!Convert.IsDBNull(dr["HinhThucThanhToan"]))
                    objcmsGioHangDO.HinhThucThanhToan = Convert.ToString(dr["HinhThucThanhToan"]);
                if (!Convert.IsDBNull(dr["ThanhTien"]))
                    objcmsGioHangDO.ThanhTien = Convert.ToInt32(dr["ThanhTien"]);
                if (!Convert.IsDBNull(dr["SessionID"]))
                    objcmsGioHangDO.SessionID = Convert.ToString(dr["SessionID"]);
                if (!Convert.IsDBNull(dr["NgayDatHang"]))
                    objcmsGioHangDO.NgayDatHang = Convert.ToDateTime(dr["NgayDatHang"]);
                if (!Convert.IsDBNull(dr["NgayGiaoHang"]))
                    objcmsGioHangDO.NgayGiaoHang = Convert.ToDateTime(dr["NgayGiaoHang"]);
                if (!Convert.IsDBNull(dr["XuLy"]))
                    objcmsGioHangDO.XuLy = Convert.ToBoolean(dr["XuLy"]);

            }
            return objcmsGioHangDO;
        }
        public DataTable GetCustomerInfo(string SessionID)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "spcmsGetCustomerInfo_BySID";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@SessionID", SqlDbType.NVarChar);
            Sqlparam.Value = SessionID;
            Sqlcomm.Parameters.Add(Sqlparam);

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }
        


        #endregion

    }

}
